/**
 * 联系人控件
 * 基于oacore,jquery-treeview.js,treeview.js
 */
;(function () {
	/**
	 * data: 初始化数据
	 * id: id号
	 * idSuffix: 节点id后缀
	 * boxClass: className
	 * treeClass: className
	 * defaultSelects
	 */
	var SID = 0;
	function AccountBook(params)
	{
		this.sid = ++ SID;
		if(!params.host || params.host.size() <= 0)
			return;
		this.pretreatment = params.pretreatment;
		this.defaultSelects = (params.defaultSelects || params.defaultSelects == 0) ? ($.isArray(params.defaultSelects) ? params.defaultSelects : [params.defaultSelects]) : [];
		var host = this;
		this.isShow = false;
		this.single = params.single;
		this.autoHideFun = function () {
			host.hide();
		};
		this.stopPropagationFun = function () {
			return false;
		};
		this.data = params.data;
		this.html = this.createHTML(params.data, params.id, params.idSuffix);
		this.box = new oacore.UI.FloatBox();
		$(document.body).append(this.box.content);
		this.box.content.addClass("oa-account-book");
		this.ul = $(this.html);
		if(params.treeClass)
			ul.addClass(params.treeClass);
		this.ul.appendTo(this.box.content).treeview();
		this.ul.hide();
		if(params.boxClass)
			box.content.addClass(boxClass);
		this.box.content.css("z-index", 100000);
		this.box.content.hide();
		//
		this.setDataForm(params.dataForm);
		this.autoHide(params.autoHide);
		this.onHide = params.onHide;
		this.onShow = params.onShow;
		this.setHost(params.host);
		setTimeout(function () {
			host.hide();
		}, 1);
	};
	
	AccountBook.prototype.show = function (pos) {
		this.isShow = true;
		this.box.content.show().offset(pos);
		this.ul.show();
	};
	
	AccountBook.prototype.destroy = function () {
		this.box.content.remove();
	};
	
	AccountBook.prototype.onHide = function () {
		
	};
	
	AccountBook.prototype.setSize = function (size) {
		if(size.width)
			this.box.content.width(size.width);
		if(size.height)
			this.box.content.height(size.height);
	};
	
	AccountBook.prototype.onShow = function () {
		
	};
	
	AccountBook.prototype.setHost = function (host, name) {
		var topHost = this;
		this.box.content.find(".-tree-node").bind("click", function (event) {
			event.stopPropagation();
		});
		host.val("");
		$(host).bind("click", function () {
				if(topHost.isShow)
					return false;
				$this = $(this);
				var pos = $this.offset();
				pos.top += $this.outerHeight() + 2;
				topHost.setSize({width: $this.innerWidth() - 7});
				topHost.show(pos);
				if(topHost.onShow)
					topHost.onShow();
				return false;
			})
			.attr("readonly", "readonly")
			.bind("keydown", function (event) {
				if(event.keyCode == 8)
					return false;
			})
			.bind("click", function (event) {
				event.stopPropagation();
			});
		var $host = $(host);
		var orgHostName = $host.attr("name");
		$host.removeAttr("name");
		var onclick = function () {
			$this = $(this);
			if($this.attr("checked"))
				$this.parent("span.treeview-has-children:first").nextAll("ul:first").find(".-tree-node").attr("checked", "checked");
			else
			{
				$this.parent("span.treeview-has-children:first").nextAll("ul:first").find(".-tree-node").attr("checked", "");
			}
			$host.val("");
			if(topHost.parentForm)
				$(topHost.parentForm).children("[name='" + orgHostName + "']").remove();
			topHost.box.content.find(".-tree-node[checked!='']:not(.parentCheckBox)").each(function (){
				var $this = $(this);
				$host.val($host.val() + $this.nextAll("span:first").html() + ";");
				if(topHost.parentForm)
					$(topHost.parentForm).append("<input type='hidden' name='" + orgHostName + "' value='"+ parseInt($this.attr("id")) +"'/>");
			});
			$host.get(0).focus();
		};
		this.box.content.find(".-tree-node").bind("click", onclick)
		.each(function (n, obj) {
			var $obj = $(obj);
			if($.inArray(parseInt($obj.attr("id")), topHost.defaultSelects) != -1)
			{
				$obj.trigger("click");
				onclick.apply(obj);
			}
		});
	};
	
	AccountBook.prototype.setDataForm = function (parentForm, accountIdName) {
		this.parentForm = parentForm;
		this.accountIdName = accountIdName;
	};
	
	AccountBook.prototype.hide = function (pos) {
		this.isShow = false;
		this.box.content.hide().offset(pos);
		this.ul.hide();
		if(this.onHide)
			this.onHide();
	};
	
	AccountBook.prototype.autoHide = function (auto) {
		if(auto)
		{
			$(document.documentElement).bind("click", this.autoHideFun);
			this.box.content.bind("click", this.stopPropagationFun);
		}
		else
		{
			$(document.documentElement).unbind("click", this.autoHideFun);
			this.box.content.unbind("click", this.stopPropagationFun);
		}
	};
	
	AccountBook.prototype.createHTML = function (data, id, idSuffix) 
	{
		//确定是否为单选联系人列表
		var type = this.single ? "radio" : "checkbox";
		var buffer = [];
		if(id)
			buffer.push("<ul id='" + id + "' class='treeData'>");
		else 
		{
			buffer.push("<ul>");
		}
		for(var i=0;i<data.length;++i)
		{
			if(this.pretreatment)
				data[i] = this.pretreatment(data[i]);
			if(!data[i])
				continue;
			buffer.push("<li>");
			if(idSuffix)
				data[i].id += idSuffix;
			else
			{
				data[i].id += "_menu_item";
			}
			if(data[i].child)
			{
				buffer.push("<span class='treeview-has-children' id='" + data[i].id + "'>");
				//为单选框的时候，父级菜单无法选择。
				if(type != "radio")
					buffer.push("<input " + (data[i].disabled ? "disabled='disabled'" : "") + " name='treeNode" + this.sid + "' type='" + type + "' id='" + data[i].id + "_checkbox' class='parentCheckBox -tree-node'>");
				buffer.push("<span id='" + data[i].id + "_value'>" + data[i].name + "</span>");
				buffer.push("</span>");
				buffer.push(this.createHTML(data[i].child, undefined, idSuffix));
			}
			else
			{
				buffer.push("<span id='" + data[i].id + "'>");
				buffer.push("<input " + (data[i].disabled ? "disabled='disabled'" : "") + " name='treeNode" + this.sid + "' type='" + type + "' id='" + data[i].id + "_checkbox' class='-tree-node'>");
				buffer.push("<span id='" + data[i].id + "_value'>" + data[i].name + "</span>");
				buffer.push("</span>");
			}
			buffer.push("</li>");
		}
		buffer.push("</ul>");
		return buffer.join("");
	};
	
	oacore.Widget.AccountBook = AccountBook;
})();